///**
// * Created by zhangxiaoyu on 2016-09-22.
// */
angular.module('hyt.hzKaiDanService', [])

    //省市区级联
    .factory('FProCitAreFactory',function($resource,$rootScope,ENV,Storage,$ionicLoading,$state){
      var api =  Storage.get('fwqdz');
      var resource = $resource(api);
      var proData={};
      var citData={};
      var areData={};
      return{
        getPro:function(sid,user_id,yh_id){
          return resource.save({
            SID:sid,
            USER_ID:user_id,
            ACTION_NAME :  'sqlKeyBiz.list#queryProvince',
            ACTION_INFO : {
              'yh_id':yh_id
            }
          },function(r){
            if(r.ACTION_RETURN_CODE == "000000"){
                proData=r.ACTION_INFO.RESULT_LIST;
            } else if (r.ACTION_RETURN_CODE == "000005"){
              $ionicLoading.show({
                noBackdrop: true,
                template: "您的账号已在其他设备登录",
                duration: 1500
              });
              $state.go("login");
            }
            $rootScope.$broadcast('FPro.proUpdated');
          });
        },
        getP : function(){
            return proData;
        },
        getCity:function(sid,user_id,yh_id,proCode){
          return resource.save({
            SID:sid,
            USER_ID:user_id,
            ACTION_NAME :  'sqlKeyBiz.list#queryCity',
            ACTION_INFO : {
              'yh_id':yh_id,
              'proCode':proCode
            }
          },function(r){
            if(r.ACTION_RETURN_CODE == "000000"){
              citData=r.ACTION_INFO.RESULT_LIST;
            } else if (r.ACTION_RETURN_CODE == "000005"){
              $ionicLoading.show({
                noBackdrop: true,
                template: "您的账号已在其他设备登录",
                duration: 1500
              });
              $state.go("login");
            }
            $rootScope.$broadcast('FCity.cityUpdated');
          });
        },
        getC : function(){
          return citData;
        },
        getAre:function(sid,user_id,yh_id,cityCode){
          return resource.save({
            SID:sid,
            USER_ID:user_id,
            ACTION_NAME :  'sqlKeyBiz.list#queryArea',
            ACTION_INFO : {
              'yh_id':yh_id,
              'cityCode':cityCode
            }
          },function(r){
            if(r.ACTION_RETURN_CODE == "000000"){
              areData=r.ACTION_INFO.RESULT_LIST;
            } else if (r.ACTION_RETURN_CODE == "000005"){
              $ionicLoading.show({
                noBackdrop: true,
                template: "您的账号已在其他设备登录",
                duration: 1500
              });
              $state.go("login");
            }
            $rootScope.$broadcast('FArea.areaUpdated');
          });
        },
        getA : function(){
          return areData;
        }
      }
    })
    .factory('SProCitAreFactory',function($resource,$rootScope,ENV,Storage,$ionicLoading,$state){
      var api =  Storage.get('fwqdz');
      var resource = $resource(api);
      var proData={};
      var citData={};
      var areData={};
      return{
        getCity:function(sid,user_id,yh_id,proCode){
          return resource.save({
            SID:sid,
            USER_ID:user_id,
            ACTION_NAME :  'sqlKeyBiz.list#queryCity',
            ACTION_INFO : {
              'yh_id':yh_id,
              'proCode':proCode
            }
          },function(r){
            if(r.ACTION_RETURN_CODE == "000000"){
              citData=r.ACTION_INFO.RESULT_LIST;
            } else if (r.ACTION_RETURN_CODE == "000005"){
              $ionicLoading.show({
                noBackdrop: true,
                template: "您的账号已在其他设备登录",
                duration: 1500
              });
              $state.go("login");
            }
            $rootScope.$broadcast('SCity.cityUpdated');
          });
        },
        getC : function(){
          return citData;
        },
        getAre:function(sid,user_id,yh_id,cityCode){
          return resource.save({
            SID:sid,
            USER_ID:user_id,
            ACTION_NAME :  'sqlKeyBiz.list#queryArea',
            ACTION_INFO : {
              'yh_id':yh_id,
              'cityCode':cityCode
            }
          },function(r){
            if(r.ACTION_RETURN_CODE == "000000"){
              areData=r.ACTION_INFO.RESULT_LIST;
            } else if (r.ACTION_RETURN_CODE == "000005"){
              $ionicLoading.show({
                noBackdrop: true,
                template: "您的账号已在其他设备登录",
                duration: 1500
              });
              $state.go("login");
            }
            $rootScope.$broadcast('SArea.areaUpdated');
          });
        },
        getA : function(){
          return areData;
        }
      }
    })
    //保存发货单
    .factory('saveFhdFactory',function($resource,$rootScope,ENV,Storage,$ionicLoading,$state){
      var api =  Storage.get('fwqdz');
      var resource = $resource(api);
      var result;
      return{
        getFhd:function(sid,user_id,yh_id,fhd,fhrq,fpro,fcity,farea,spro,scity,sarea,fproCode,fcityCode,fareaCode,sproCode,scityCode,sareaCode,shfs,lxdh,fhdzt,fhdlx){
          return resource.save({
            SID:sid,
            USER_ID:user_id,
            ACTION_NAME :  'sqlKeyBiz.transInsert#addFhd',
            ACTION_INFO : {
              'yh_id':yh_id,
              'lxdh':lxdh,
              'fhdzt':fhdzt,
              'fhdlx':fhdlx,
              'shfs':shfs,
              'fhrmc':fhd.fhrmc,
              'fhrdh':fhd.fhrdh,
              'fpro':fpro,
              'fcity':fcity,
              'farea':farea,
              'spro':spro,
              'scity':scity,
              'sarea':sarea,
              'fprocode':fproCode,
              'fcitycode':fcityCode,
              'fareacode':fareaCode,
              'sprocode':sproCode,
              'scitycode':scityCode,
              'sareacode':sareaCode,
              'fhrdz':fhd.fhrdz,
              'shrmc':fhd.shrmc,
              'shrdh':fhd.shrdh,
              'shrdz':fhd.shrdz,
              'hwmc':fhd.hwmc,
              'fhrq':fhrq,
              'zl':fhd.zl,
              'sl':fhd.sl,
              'tj':fhd.tj,
              'dshk':fhd.dshk,
              'bjsm':fhd.bjsm,
              'tbzt':fhd.isTb,
              'sfhd':fhd.isHd,
              'bz':fhd.bz
            }
          },function(r){
            console.log(r);
            if(r.ACTION_RETURN_CODE == "000000"){
              result= r.ACTION_INFO;
            } else if (r.ACTION_RETURN_CODE == "000005"){
              $ionicLoading.show({
                noBackdrop: true,
                template: "您的账号已在其他设备登录",
                duration: 1500
              });
              $state.go("login");
            }
            $rootScope.$broadcast('Save.fhdUpdated');
          });
        },
        getResult : function(){
          return result;
        }
      }
    })
    //提交撤销发货单
    .factory('submitFhdFactory',function($resource,$rootScope,ENV,Storage,$ionicLoading,$state){
      var api =  Storage.get('fwqdz');
      var resource = $resource(api);
      var result,resultRecall;
      return{
        getSubmitFhd:function(sid,user_id,yh_id,lxdh,fhd_id,zdxx,fhdzt){
          return resource.save({
            SID:sid,
            USER_ID:user_id,
            ACTION_NAME :  'sqlKeyBiz.transUpdate#submitFhd',
            ACTION_INFO : {
              'yh_id':yh_id,
              'lxdh':lxdh,
              'fhdzt':fhdzt,
              'fhd_id':fhd_id,
              'dw_id':zdxx.dw_id,
              'dwmc':zdxx.dwmc,
              'zd_id':zdxx.zd_id,
              'zdmc':zdxx.zdmc,
              'xl':zdxx.xl,
              'zddh':zdxx.zddh
            }
          },function(r){
            if(r.ACTION_RETURN_CODE == "000000"){
              result= r.ACTION_INFO.result;
            } else if (r.ACTION_RETURN_CODE == "000005"){
              $ionicLoading.show({
                noBackdrop: true,
                template: "您的账号已在其他设备登录",
                duration: 1500
              });
              $state.go("login");
            }
            $rootScope.$broadcast('Submit.fhdUpdated');
          });
        },
        getResult : function(){
          return result;
        },
        getRecallFhd:function(sid,user_id,yh_id,lxdh,fhd_id,dd_id){
          return resource.save({
            SID:sid,
            USER_ID:user_id,
            ACTION_NAME :  'sqlKeyBiz.transUpdate#recallFhd',
            ACTION_INFO : {
              'yh_id':yh_id,
              'lxdh':lxdh,
              'fhd_id':fhd_id,
              'dd_id':dd_id,
              'dw_id':'',
              'dwmc':'',
              'zd_id':'',
              'zdmc':''
            }
          },function(r){
            if(r.ACTION_RETURN_CODE == "000000"){
              resultRecall= r.ACTION_INFO.result;
            } else if (r.ACTION_RETURN_CODE == "000005"){
              $ionicLoading.show({
                noBackdrop: true,
                template: "您的账号已在其他设备登录",
                duration: 1500
              });
              $state.go("login");
            }
            $rootScope.$broadcast('Recall.fhdUpdated');
          });
        },
        getResultRecall : function(){
          return resultRecall;
        }
      }
    })
    //保存补单
    .factory('saveBdFactory',function($resource,$rootScope,ENV,Storage,$ionicLoading,$state){
      var api =  Storage.get('fwqdz');
      var resource = $resource(api);
      var result,savaPic,deletePic,count;
      return{
        getBd:function(sid,user_id,yh_id,buDan,cyrq,fpro,fcity,farea,spro,scity,sarea,fproCode,fcityCode,fareaCode,sproCode,scityCode,sareaCode,shfs,lxdh,fhdzt,fhdlx){
          return resource.save({
            SID:sid,
            USER_ID:user_id,
            ACTION_NAME :  'sqlKeyBiz.transInsert#addFhd',
            ACTION_INFO : {
              'yh_id':yh_id,
              'lxdh':lxdh,
              'cydh':buDan.cydh,
              'dwmc':buDan.dwmc,
              'cyrq':cyrq,
              'thf':buDan.thf,
              'fhdlx':fhdlx,
              'fhdzt':fhdzt,
              'shfs':shfs,
              'fhrmc':buDan.fhrmc,
              'fhrdh':buDan.fhrdh,
              'fpro':fpro,
              'fcity':fcity,
              'farea':farea,
              'spro':spro,
              'scity':scity,
              'sarea':sarea,
              'fprocode':fproCode,
              'fcitycode':fcityCode,
              'fareacode':fareaCode,
              'sprocode':sproCode,
              'scitycode':scityCode,
              'sareacode':sareaCode,
              'fhrdz':buDan.fhrdz,
              'shrmc':buDan.shrmc,
              'shrdh':buDan.shrdh,
              'shrdz':buDan.shrdz,
              'hwmc':buDan.hwmc,
              'zl':buDan.zl,
              'sl':buDan.sl,
              'tj':buDan.tj,
              'bzlx':buDan.bzlx,
              'yf_xf':buDan.yf_xf,
              'yf_hf':buDan.yf_hf,
              'yf_yj':buDan.yf_yj,
              'yf_tf':buDan.yf_tf,
              'yf_ycdk':buDan.yf_ycdk,
              'xxf_xf':buDan.xxf_xf,
              'xxf_qf':buDan.xxf_qf,
              'dshk':buDan.dsk_hf,
              'shf':buDan.shf,
              'bjsm':buDan.be,
              'bf':buDan.bf,
              'tbzt':buDan.tbzt,
              'sfhd':buDan.sfhd,
              'yf_df':buDan.yf_df,
              'dsk_df':buDan.dsk_xf,
              'bz':buDan.bz
            }
          },function(r){
            console.log(r);
            if(r.ACTION_RETURN_CODE == "000000"){
              result= r.ACTION_INFO;
            } else if (r.ACTION_RETURN_CODE == "000005"){
              $ionicLoading.show({
                noBackdrop: true,
                template: "您的账号已在其他设备登录",
                duration: 1500
              });
              $state.go("login");
            }
            $rootScope.$broadcast('Save.bdUpdated');
          });
        },
        getResult : function(){
          return result;
        },
        getSavaPic:function(sid,user_id,yh_id,imageStr,fhd_id,ext,extendName){
          return resource.save({
            SID:sid,
            USER_ID:user_id,
            ACTION_NAME :  'yanzhengBiz.uploadPIC#insert',
            ACTION_INFO : {
              'yh_id':yh_id,
              'imageStr':imageStr,
              'fhd_id':fhd_id,
              'ext':ext,
              'extendName':extendName
            }
          },function(r){
            if(r.ACTION_RETURN_CODE == "000000"){
              savaPic= 1;
            } else if (r.ACTION_RETURN_CODE == "000005"){
              $ionicLoading.show({
                noBackdrop: true,
                template: "您的账号已在其他设备登录",
                duration: 1500
              });
              $state.go("login");
            }
            $rootScope.$broadcast('Save.picUpdated');
          });
        },
        getSavePicResult : function(){
          return savaPic;
        },
        getDeletePic:function(sid,user_id,yh_id,id){
          return resource.save({
            SID:sid,
            USER_ID:user_id,
            ACTION_NAME :  'beanBiz.delete#Bd_Image',
            ACTION_INFO : {
              'yh_id':yh_id,
              'id':id
            }
          },function(r){
            if(r.ACTION_RETURN_CODE == "000000"){
              deletePic= 1;
            } else if (r.ACTION_RETURN_CODE == "000005"){
              $ionicLoading.show({
                noBackdrop: true,
                template: "您的账号已在其他设备登录",
                duration: 1500
              });
              $state.go("login");
            }
            $rootScope.$broadcast('Delete.picUpdated');
          });
        },
        getDeletePicResult : function(){
          return deletePic;
        },
        checkCydhExist:function(sid,user_id,yh_id,cydh,dwmc,fhd_id){
          return resource.save({
            SID:sid,
            USER_ID:user_id,
            ACTION_NAME :  'sqlKeyBiz.list#count_fhd_by_cydh',
            ACTION_INFO : {
              'yh_id':yh_id,
              'cydh':cydh,
              'dwmc':dwmc,
              'fhd_id':fhd_id
            }
          },function(r){
            if(r.ACTION_RETURN_CODE == "000000"){
              count= r.ACTION_INFO.RESULT_LIST[0].cn;
            } else if (r.ACTION_RETURN_CODE == "000005"){
              $ionicLoading.show({
                noBackdrop: true,
                template: "您的账号已在其他设备登录",
                duration: 1500
              });
              $state.go("login");
            }
            $rootScope.$broadcast('Cydh.check');
          });
        },
        getCount : function(){
          return count;
        }
      }
    })
    //物流公司选择
    .factory('LszdFactory',function($resource,$rootScope,ENV,Storage,$ionicLoading,$state){
      var api =  Storage.get('fwqdz');
      var resource = $resource(api);
      var lszdData={},
          pagesize=10;
      return{
        getLsfhzd:function(sid,user_id,yh_id,lxdh,mc){
          var hasNextPage =true;    //判断是否有下一页
          return resource.save({
            SID:sid,
            USER_ID:user_id,
            ACTION_NAME :  'sqlKeyBiz.page#queryLszd',
            ACTION_INFO : {
              'yh_id':yh_id,
              'lxdh':lxdh,
              'mc':mc,
              'pagesize':'10',
              'current_page':'1'
            }
          },function(r){
            if(r.ACTION_RETURN_CODE == "000000"){
              if(r.ACTION_INFO.RESULT_LIST.length < pagesize){
                hasNextPage = false;
              }
              lszdData[pagesize] = {
                hasNextPage : hasNextPage,
                'current_page' : 2,
                'data' : r.ACTION_INFO.RESULT_LIST
              };

              $rootScope.$broadcast('LszdList.lszdUpdated');
            }else if (r.ACTION_RETURN_CODE == "000005"){
              $ionicLoading.show({
                noBackdrop: true,
                template: "您的账号已在其他设备登录",
                duration: 1500
              });
              $state.go("login");
            }
          });
        },
        getLszd : function(){
          if(lszdData[pagesize] == undefined){
            return false;
          }
          return lszdData[pagesize];
        },
        getMoreLszd:function(sid,user_id,yh_id,lxdh,mc){
          //为了解决一步加载的时候数据还没有加载完成  然后请求loadMore的时候  找不到数据
          if(lszdData[pagesize] == undefined){
            return false;
          }
          //获取以前的数据
          var hasNextPage = lszdData[pagesize].hasNextPage;
          var current_page = lszdData[pagesize].current_page;
          var moreLszdData = lszdData[pagesize].data;

          resource.save({
            ACTION_NAME: 'sqlKeyBiz.page#queryLszd',
            SID:sid,
            USER_ID:user_id,
            ACTION_INFO : {
              'yh_id':yh_id,
              'lxdh':lxdh,
              'mc':mc,
              'pagesize':pagesize,
              'current_page':current_page
            }
          }, function (r) {

            current_page++;

            if(r.ACTION_RETURN_CODE == "000000"){
              if(r.ACTION_INFO.RESULT_LIST.length < pagesize){
                hasNextPage = false;
              }else if (r.ACTION_RETURN_CODE == "000005"){
                $ionicLoading.show({
                  noBackdrop: true,
                  template: "您的账号已在其他设备登录",
                  duration: 1500
                });
                $state.go("login");
              }

              moreLszdData = moreLszdData.concat(r.ACTION_INFO.RESULT_LIST);

              lszdData[pagesize] = {
                hasNextPage : hasNextPage,
                'current_page' : current_page,
                'data' : moreLszdData
              };
              $rootScope.$broadcast('LszdList.lszdUpdated');
            }

          })
        },

        hasNextPage: function() {
          if (lszdData[pagesize] === undefined) {
            return false;
          }
          return lszdData[pagesize].hasNextPage;
        }
      }
    })
    .factory('CityzdFactory',function($resource,$rootScope,ENV,Storage,$ionicLoading,$state){
      var api =  Storage.get('fwqdz');
      var resource = $resource(api);
      var cityzdData={},
          pagesize=10;
      return{
        getCityzd:function(sid,user_id,yh_id,zdmc,mc){
          var hasNextPage =true;    //判断是否有下一页
          return resource.save({
            SID:sid,
            USER_ID:user_id,
            ACTION_NAME :  'sqlKeyBiz.page#queryDzzd',
            ACTION_INFO : {
              'yh_id':yh_id,
              'zdmc':zdmc,
              'mc':mc,
              'pagesize':'10',
              'current_page':'1'
            }
          },function(r){
            console.log(r);
            if(r.ACTION_RETURN_CODE == "000000"){
              if(r.ACTION_INFO.RESULT_LIST.length < pagesize){
                hasNextPage = false;
              }
              cityzdData[pagesize] = {
                hasNextPage : hasNextPage,
                'current_page' : 2,
                'data' : r.ACTION_INFO.RESULT_LIST
              };

              $rootScope.$broadcast('CityzdList.cityzdUpdated');
            }else if (r.ACTION_RETURN_CODE == "000005"){
              $ionicLoading.show({
                noBackdrop: true,
                template: "您的账号已在其他设备登录",
                duration: 1500
              });
              $state.go("login");
            }
          });
        },
        getCity : function(){
          if(cityzdData[pagesize] == undefined){
            return false;
          }
          return cityzdData[pagesize];
        },
        getMoreCityzd:function(sid,user_id,yh_id,zdmc,mc){
          //为了解决一步加载的时候数据还没有加载完成  然后请求loadMore的时候  找不到数据
          if(cityzdData[pagesize] == undefined){
            return false;
          }
          //获取以前的数据
          var hasNextPage = cityzdData[pagesize].hasNextPage;
          var current_page = cityzdData[pagesize].current_page;
          var moreCityzdData = cityzdData[pagesize].data;

          resource.save({
            ACTION_NAME: 'sqlKeyBiz.page#queryDzzd',
            SID:sid,
            USER_ID:user_id,
            ACTION_INFO : {
              'yh_id':yh_id,
              'zdmc':zdmc,
              'mc':mc,
              'pagesize':pagesize,
              'current_page':current_page
            }
          }, function (r) {

            current_page++;

            if(r.ACTION_RETURN_CODE == "000000"){
              if(r.ACTION_INFO.RESULT_LIST.length < pagesize){
                hasNextPage = false;
              }else if (r.ACTION_RETURN_CODE == "000005"){
                $ionicLoading.show({
                  noBackdrop: true,
                  template: "您的账号已在其他设备登录",
                  duration: 1500
                });
                $state.go("login");
              }

              moreCityzdData = moreCityzdData.concat(r.ACTION_INFO.RESULT_LIST);

              cityzdData[pagesize] = {
                hasNextPage : hasNextPage,
                'current_page' : current_page,
                'data' : moreCityzdData
              };
              $rootScope.$broadcast('CityzdList.cityzdUpdated');
            }

          })
        },

        hasNextPage: function() {
          if (cityzdData[pagesize] === undefined) {
            return false;
          }
          return cityzdData[pagesize].hasNextPage;
        }
      }
    })
    .factory('OftenzdFactory',function($resource,$rootScope,ENV,Storage,$ionicLoading,$state){
      var api =  Storage.get('fwqdz');
      var resource = $resource(api);
      var oftenzdData={},
          pagesize=10;
      var result;
      return{
        getOftenzd:function(sid,user_id,yh_id,mc){
          var hasNextPage =true;    //判断是否有下一页
          return resource.save({
            SID:sid,
            USER_ID:user_id,
            ACTION_NAME :  'sqlKeyBiz.page#queryCyzd',
            ACTION_INFO : {
              'yh_id':yh_id,
              'mc':mc,
              'pagesize':'10',
              'current_page':'1'
            }
          },function(r){
            console.log(r);
            if(r.ACTION_RETURN_CODE == "000000"){
              if(r.ACTION_INFO.RESULT_LIST.length < pagesize){
                hasNextPage = false;
              }
              oftenzdData[pagesize] = {
                hasNextPage : hasNextPage,
                'current_page' : 2,
                'data' : r.ACTION_INFO.RESULT_LIST
              };

              $rootScope.$broadcast('OftenzdList.oftenzdUpdated');
            }else if (r.ACTION_RETURN_CODE == "000005"){
              $ionicLoading.show({
                noBackdrop: true,
                template: "您的账号已在其他设备登录",
                duration: 1500
              });
              $state.go("login");
            }
          });
        },
        getOften : function(){
          if(oftenzdData[pagesize] == undefined){
            return false;
          }
          return oftenzdData[pagesize];
        },
        getMoreOftenzd:function(sid,user_id,yh_id,mc){
          //为了解决一步加载的时候数据还没有加载完成  然后请求loadMore的时候  找不到数据
          if(oftenzdData[pagesize] == undefined){
            return false;
          }
          //获取以前的数据
          var hasNextPage = oftenzdData[pagesize].hasNextPage;
          var current_page = oftenzdData[pagesize].current_page;
          var moreOftenzdData = oftenzdData[pagesize].data;

          resource.save({
            ACTION_NAME: 'sqlKeyBiz.page#queryCyzd',
            SID:sid,
            USER_ID:user_id,
            ACTION_INFO : {
              'yh_id':yh_id,
              'mc':mc,
              'pagesize':pagesize,
              'current_page':current_page
            }
          }, function (r) {

            current_page++;

            if(r.ACTION_RETURN_CODE == "000000"){
              if(r.ACTION_INFO.RESULT_LIST.length < pagesize){
                hasNextPage = false;
              }else if (r.ACTION_RETURN_CODE == "000005"){
                $ionicLoading.show({
                  noBackdrop: true,
                  template: "您的账号已在其他设备登录",
                  duration: 1500
                });
                $state.go("login");
              }

              moreOftenzdData = moreOftenzdData.concat(r.ACTION_INFO.RESULT_LIST);

              oftenzdData[pagesize] = {
                hasNextPage : hasNextPage,
                'current_page' : current_page,
                'data' : moreOftenzdData
              };
              $rootScope.$broadcast('OftenzdList.oftenzdUpdated');
            }

          })
        },

        hasNextPage: function() {
          if (oftenzdData[pagesize] === undefined) {
            return false;
          }
          return oftenzdData[pagesize].hasNextPage;
        },

        getDelete:function(sid,user_id,yh_id,id){
          return resource.save({
            SID:sid,
            USER_ID:user_id,
            ACTION_NAME :  'beanBiz.delete#Wlgs_zdxx',
            ACTION_INFO : {
              'yh_id':yh_id,
              'id':id
            }
          },function(r){
            console.log(r);
            if(r.ACTION_RETURN_CODE == "000000") {
              result = r.ACTION_INFO.result;
              $rootScope.$broadcast('DeleteWlgs');
            }
          });
        },
        deleteResult:function(){
          return result;
        }
      }
    })
    .factory('AllzdFactory',function($resource,$rootScope,ENV,Storage,$ionicLoading,$state){
      var api =  Storage.get('fwqdz');
      var resource = $resource(api);
      var allzdData={},
          pagesize=10;
      return{
        getAllzd:function(sid,user_id,yh_id,content){
          var hasNextPage =true;    //判断是否有下一页
          return resource.save({
            SID:sid,
            USER_ID:user_id,
            ACTION_NAME :  'sqlKeyBiz.page#queryAllgszd',
            ACTION_INFO : {
              'yh_id':yh_id,
              'mc':content,
              'pagesize':'10',
              'current_page':'1'
            }
          },function(r){
            console.log(r);
            if(r.ACTION_RETURN_CODE == "000000"){
              if(r.ACTION_INFO.RESULT_LIST.length < pagesize){
                hasNextPage = false;
              }
              allzdData[pagesize] = {
                hasNextPage : hasNextPage,
                'current_page' : 2,
                'data' : r.ACTION_INFO.RESULT_LIST
              };

              $rootScope.$broadcast('AllzdList.allzdUpdated');
            }else if (r.ACTION_RETURN_CODE == "000005"){
              $ionicLoading.show({
                noBackdrop: true,
                template: "您的账号已在其他设备登录",
                duration: 1500
              });
              $state.go("login");
            }
          });
        },
        getAll : function(){
          if(allzdData[pagesize] == undefined){
            return false;
          }
          return allzdData[pagesize];
        },
        getMoreAllzd:function(sid,user_id,yh_id,content){
          //为了解决一步加载的时候数据还没有加载完成  然后请求loadMore的时候  找不到数据
          if(allzdData[pagesize] == undefined){
            return false;
          }
          //获取以前的数据
          var hasNextPage = allzdData[pagesize].hasNextPage;
          var current_page = allzdData[pagesize].current_page;
          var moreAllzdData = allzdData[pagesize].data;

          resource.save({
            ACTION_NAME: 'sqlKeyBiz.page#queryAllgszd',
            SID:sid,
            USER_ID:user_id,
            ACTION_INFO : {
              'yh_id':yh_id,
              'mc':content,
              'pagesize':pagesize,
              'current_page':current_page
            }
          }, function (r) {

            current_page++;

            if(r.ACTION_RETURN_CODE == "000000"){
              if(r.ACTION_INFO.RESULT_LIST.length < pagesize){
                hasNextPage = false;
              }else if (r.ACTION_RETURN_CODE == "000005"){
                $ionicLoading.show({
                  noBackdrop: true,
                  template: "您的账号已在其他设备登录",
                  duration: 1500
                });
                $state.go("login");
              }

              moreAllzdData = moreAllzdData.concat(r.ACTION_INFO.RESULT_LIST);

              allzdData[pagesize] = {
                hasNextPage : hasNextPage,
                'current_page' : current_page,
                'data' : moreAllzdData
              };
              $rootScope.$broadcast('AllzdList.allzdUpdated');
            }

          })
        },

        hasNextPage: function() {
          if (allzdData[pagesize] === undefined) {
            return false;
          }
          return allzdData[pagesize].hasNextPage;
        }
      }
    })
    //添加物流公司
    .factory('addCompanyFactory',function($resource,$rootScope,ENV,Storage,$ionicLoading,$state){
      var api =  Storage.get('fwqdz');
      var resource = $resource(api);
      var result;
      return{
        getAddCompany:function(sid,user_id,yh_id,zdxx){
          return resource.save({
            SID:sid,
            USER_ID:user_id,
            ACTION_NAME :  'sqlKeyBiz.transInsert#addWlgs',
            ACTION_INFO : {
              'yh_id':yh_id,
              'dw_id':zdxx.dw_id,
              'zd_id':zdxx.zd_id,
              'dwmc':zdxx.dwmc,
              'zdmc':zdxx.zdmc,
              'zddh':zdxx.zddh,
              'xl':zdxx.xl
            }
          },function(r){
            if(r.ACTION_RETURN_CODE == "000000"){
              result= r.ACTION_INFO.result;
            } else if (r.ACTION_RETURN_CODE == "000005"){
              $ionicLoading.show({
                noBackdrop: true,
                template: "您的账号已在其他设备登录",
                duration: 1500
              });
              $state.go("login");
            }
            $rootScope.$broadcast('Add.companyUpdated');
          });
        },
        getResult : function(){
          return result;
        }
      }
    })
    //联系人选择
    .factory('HzShrFactory',function($resource,$rootScope,ENV,Storage,$ionicLoading,$state){
      var api =  Storage.get('fwqdz');
      var resource = $resource(api);
      var result;
      var hzShrData={},
          pagesize=10;
      return{
        getcontent:function(sid,user_id,yh_id,khmc,lxdh,pyjm){
          var hasNextPage =true;    //判断是否有下一页
          return resource.save({
            SID:sid,
            USER_ID:user_id,
            ACTION_NAME :  'sqlKeyBiz.page#querySfhrxx',
            ACTION_INFO : {
              'yh_id':yh_id,
              'name':khmc,
              'lxdh':lxdh,
              'pyjm':pyjm,
              'pagesize':'10',
              'current_page':'1'
            }
          },function(r){
            if(r.ACTION_RETURN_CODE == "000000"){
              if(r.ACTION_INFO.RESULT_LIST.length < pagesize){
                hasNextPage = false;
              }

              hzShrData[pagesize] = {
                hasNextPage : hasNextPage,
                'current_page' : 2,
                'data' : r.ACTION_INFO.RESULT_LIST
              };

              $rootScope.$broadcast('ShrList.shrUpdated');
            }else if (r.ACTION_RETURN_CODE == "000005"){
              $ionicLoading.show({
                noBackdrop: true,
                template: "您的账号已在其他设备登录",
                duration: 1500
              });
              $state.go("login");
            }
          });
        },
        getShr : function(){
          if(hzShrData[pagesize] == undefined){
            return false;
          }
          return hzShrData[pagesize];
        },
        getMoreShr:function(sid,user_id,yh_id,khmc,lxdh,pyjm){
          //为了解决一步加载的时候数据还没有加载完成  然后请求loadMore的时候  找不到数据
          if(hzShrData[pagesize] == undefined){
            return false;
          }

          //获取以前的数据
          var hasNextPage = hzShrData[pagesize].hasNextPage;
          var current_page = hzShrData[pagesize].current_page;
          var moreManListData = hzShrData[pagesize].data;

          resource.save({
            ACTION_NAME: 'sqlKeyBiz.page#querySfhrxx',
            SID:sid,
            USER_ID:user_id,
            ACTION_INFO : {
              'yh_id':yh_id,
              'khmc':khmc,
              'lxdh':lxdh,
              'pyjm':pyjm,
              'pagesize':pagesize,
              'current_page':current_page
            }
          }, function (r) {

            current_page++;

            if(r.ACTION_RETURN_CODE == "000000"){
              if(r.ACTION_INFO.RESULT_LIST.length < pagesize){
                hasNextPage = false;
              }else if (r.ACTION_RETURN_CODE == "000005"){
                $ionicLoading.show({
                  noBackdrop: true,
                  template: "您的账号已在其他设备登录",
                  duration: 1500
                });
                $state.go("login");
              }

              moreManListData = moreManListData.concat(r.ACTION_INFO.RESULT_LIST);

              hzShrData[pagesize] = {
                hasNextPage : hasNextPage,
                'current_page' : current_page,
                'data' : moreManListData
              };
              $rootScope.$broadcast('ShrList.shrUpdated');
            }

          })
        },

        hasNextPage: function() {
          if (hzShrData[pagesize] === undefined) {
            return false;
          }
          return hzShrData[pagesize].hasNextPage;
        },

        getDelete:function(sid,user_id,yh_id,id){
          return resource.save({
            SID:sid,
            USER_ID:user_id,
            ACTION_NAME :  'beanBiz.delete#Sfhrdz',
            ACTION_INFO : {
              'yh_id':yh_id,
              'id':id
            }
          },function(r){
            if(r.ACTION_RETURN_CODE == "000000") {
              result = r.ACTION_INFO.result;
              $rootScope.$broadcast('DeleteData');
            }
          });
        },
        deleteResult:function(){
          return result;
        }
      }
    });
